package model;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


/**
 * @author Zach Michaelov
 */
public abstract class FileParser {
    private List<String> headers;
    private Scanner input;

    public FileParser(File dataFile) {
        headers = new ArrayList<String>();

        try {
            input = new Scanner(dataFile);

            if (input.hasNextLine()) {
                populateHeaders(input);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();  // should not happen, because the file is chosen by the user
        }

    }

    public List<String> getHeaders() {
        return headers;
    }

    public boolean hasHeaders() {
        return !headers.isEmpty();
    }

    private void populateHeaders(Scanner input) {
        String[] temp = input.nextLine().split(getDelimiter());
        for (String s : temp) {
            headers.add(s);
        }
    }

    public abstract String getDelimiter();


    public boolean hasNexRow() {
        return input.hasNextLine();
    }

    /**
     * called when the file parsing is complete
     */
    public void close() {
        input.close();
    }

    /**
     * returns the values of the next row in the file, as a list of Strings
     *
     * @return
     */
    public List<String> getNextRow() {
        String row = input.nextLine();
        String[] rowVals = row.split(getDelimiter());
        ArrayList<String> rowValList = new ArrayList<String>();
        for (String rowVal : rowVals) {
            rowValList.add(rowVal);
        }
        return rowValList;
    }

}
